package com.dy.nygl.controller;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.dy.nygl.mapper.CostAnalysisMapper;
import com.dy.nygl.service.CostAnalysisService;
import com.dy.nygl.service.MachineService;

/**
 * 查询的连贯性?
 * 本类实现内容：machine仅作为查询，不存入report 总成本表
 * selectTaskName.action  ----- > 任务名的查询内容。
 * selectObject.action  ----- > 时间和机器名的查询。
 * costAndmachineEnerty.action  ----- > 查询某台机器一年，一月，一日的能耗管理。输入时间。
 * costAndMachineTask.action  ----- > 查询某个项目,准确任务名。
 * costAndMachineTime.action  ----- > 对时间的查找,2016 2016-08 2016-08-10。
 * @author Administrator
 */
//String json = JSON.toJSONString(map);用于转化为json

@Controller
@RequestMapping("/machine")
public class MachineController {
	
	@Resource(name="machineService")
	private MachineService machineService;
	
	@Resource(name="costAnalysisService")
	private CostAnalysisService costAnalysisService;
	
	@Resource(name="costAnalysisDao")
	private CostAnalysisMapper costAnalysisDao;
	
	@RequestMapping(value="/indexMachine.action",method={RequestMethod.GET})
	public ModelAndView indexMachine(){
		System.err.println("进入machine。。。");
		ModelAndView model = new ModelAndView();
		model.setViewName("machine");
		return model;
	}
	
	/** 任务名 查询各能源总消耗,页面返回问题,按照顺序获取
	 */
	@RequestMapping("/selectTaskName.action")
	@ResponseBody
	public Map<String,Object> selectTaskName(@RequestParam("taskname")String taskName,HttpServletRequest request) throws ParseException, UnsupportedEncodingException{
		request.setCharacterEncoding("utf-8");
		Map<String,Object> map = new LinkedHashMap<String, Object>();
		List<String> listType = costAnalysisDao.selectEnergyType();
		List<Double> machineTaskName = machineService.selectTaskName(taskName);
		System.err.println("任务名="+machineTaskName);
		map.put("type", listType);
		map.put("sum", machineTaskName);
		map.put("taskname", taskName);
		System.err.println(map);
		return map;
	}
	
	/** 按照机器名、能源进行查找，获取提交的内容
	 */
	@RequestMapping("/selectObject.action")
	@ResponseBody
	public Map<String,Object> selectObject(@RequestParam("object")String object,@RequestParam("time")String time) throws ParseException{
		System.err.println(time);
		List<Double> machineObject = machineService.selectMachine(object, time);
		Map<String,Object> map = new LinkedHashMap<String, Object>();
		List<String> xAxis = new ArrayList<>();
		String ems = null ;
		if(time.length()<5){
			xAxis = machineService.hcYear();
		}else if(time.length()<8){
			xAxis = machineService.hcMonth();
		}else if(time.length()<11){
			xAxis = machineService.hcDay();
		}
		char[] num = object.toCharArray();
		if(Character.isDigit(num[0])){
			System.out.println("非任务名。。。");
			ems = costAnalysisDao.selectTypeByCode(object);
		} else {
			System.out.println("非任务名。。。");
			ems = object;
		}
		map.put("machineObject",machineObject);
		map.put("ems",ems);
		map.put("xAxis",xAxis);
		System.out.println(map);
		return map;
	}
	
	@RequestMapping("/selectMonthByOneTime.action")
	@ResponseBody
	public Map<String,Object> selectMonthByOneTime(@RequestParam("startTime")String starttime) throws ParseException{
		Map<String,Object> map = new LinkedHashMap<String, Object>();
		List<Double> Onetime = machineService.monthByOneTime(starttime);
		map.put("Onetime", Onetime);
		return map;
	}
	
	/** 对应时间进行 成本统计（对指定能源进行统计）
	 */
	@RequestMapping("/costAndMachineTime.action")
	@ResponseBody
	public Map<String,Object> costAndMachineTime(@RequestParam("time")String time) throws ParseException{
		List<Double> timeCost = machineService.costAndMachineTime(time);
		List<String> listType = costAnalysisDao.selectEnergyType();
		Map<String,Object> costTime = new LinkedHashMap<>();
		costTime.put("listType", listType);
		costTime.put("timeCost", timeCost);
		costTime.put("time", time);
		System.out.println("costAndMachineTime="+costTime);
		return costTime;
	}
	
	/** 任务名成本统计（对指定能源进行统计）
	 */
	@RequestMapping("/costAndMachineTask.action")
	@ResponseBody
	public Map<String,Object> costAndMachineTask(@RequestParam("taskname")String taskName) throws ParseException{
		Map<String,Object> costTask = new LinkedHashMap<>();
		System.err.println("costAndMachineTask="+taskName);
		List<Double> taskNameCost = machineService.costAndMachineTask(taskName);
		List<String> listType = costAnalysisDao.selectEnergyType();
		costTask.put("listType", listType);
		costTask.put("taskName", taskName);
		costTask.put("taskNameCost", taskNameCost);
		System.err.println("cost="+costTask);
		return costTask;
	}
	
	/** 能源成本统计（对指定能源进行统计）
	 */
	@RequestMapping("/costAndmachineEnerty.action")
	public ModelAndView costAndmachineEnerty(@RequestParam("machine")String machine,@RequestParam("enerty") String object){
		Double code = costAnalysisDao.selectPriceByCode(object);// 获取价格，
		machine = machine.substring(1, machine.length()-1);
		String[] str = machine.split(",");
		List<String> list = Arrays.asList(str);
		ModelAndView model =  new ModelAndView();
		model.addObject("costEnerty",machineService.costPriceByEnerty(list, code));
		return model;
	}
	
	@RequestMapping("/selectMonthBySetTime.action")
	@ResponseBody
	public Map<String,Object> selectMonthBySetTime(@RequestParam("startTime")String starttime, @RequestParam("endTime")String endtime) throws ParseException{
		System.out.println(starttime+","+endtime);
		Map<String,Object> map = new LinkedHashMap<String, Object>();
		List<String> xAxis = new ArrayList<>();
		if(starttime.length()<5){
			System.out.println("判断为年");
			xAxis = machineService.hcYear();
		} else {
			System.out.println("判断为月");
			xAxis = machineService.hcMonth();
		}
		List<String> listType = costAnalysisDao.selectEnergyType();
		System.out.println(listType);
		List<Double> SetTime = machineService.monthBySetTime(starttime,endtime);
		System.err.println(SetTime);
		
		map.put("xAxis", xAxis);
		map.put("SetTime", SetTime);
		map.put("listType", listType);
		map.put("settime", starttime+ "——"+ endtime);
		return map;
	}
	
	/** 给定任意时间段的能源类型的汇总
	 */
	@RequestMapping("/selectTimeRandom.action")
	@ResponseBody
	public ModelAndView selectTimeRandom(HttpServletRequest request) throws ParseException, UnsupportedEncodingException{
		request.setCharacterEncoding("utf-8");
		List<Double> timeRandom = machineService.randomTime(request.getParameter("startTime"), request.getParameter("endTime"));
		ModelAndView model = new ModelAndView();
		model.addObject("timeRandom",timeRandom);
		return model;
	}
}

